using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace LS.Setting
{
    public partial class ApplicationSetting
    {
        public class AudioSetting : SettingBase
        {
            private ModifiableValue<bool> m_MuteMusic;
            private ModifiableValue<bool> m_MuteSound;
            private ModifiableValue<float> m_MusicVolume;
            private ModifiableValue<float> m_SoundVolume;

            /// <summary>
            /// 是否开启音乐
            /// </summary>
            public bool MuteMusic
            {
                get
                {
                    return m_MuteMusic;
                }
                set
                {
                    m_MuteMusic.SetValue(value);
                }
            }

            /// <summary>
            /// 是否开启音效
            /// </summary>
            public bool MuteSound
            {
                get
                {
                    return m_MuteSound;
                }
                set
                {
                    m_MuteSound.SetValue(value);
                }
            }

            /// <summary>
            /// 音乐音量
            /// </summary>
            public float MusicVolume
            {
                get
                {
                    return m_MusicVolume;
                }
                set
                {
                    m_MusicVolume.SetValue(value);
                }
            }

            /// <summary>
            /// 音效音量
            /// </summary>
            public float SoundVolume
            {
                get
                {
                    return m_SoundVolume;
                }
                set
                {
                    m_SoundVolume.SetValue(value);
                }
            }
            /// <summary>
            /// 是否有值被修改
            /// </summary>
            public override bool IsValueChanged
            {
                get
                {
                    return
                    m_MuteMusic.IsValueChanged ||
                    m_MuteSound.IsValueChanged ||
                    m_MusicVolume.IsValueChanged ||
                    m_SoundVolume.IsValueChanged;

                }
            }

            public AudioSetting()
            {
                Load();
            }

            /// <summary>
            /// 加载设置
            /// </summary>
            public override void Load()
            {
                SoundComponent sound = LSEntry.Sound;
                if (sound != null)
                {
                    m_MuteMusic = (ModifiableValue<bool>)sound.IsMuted("Music");
                    m_MuteSound = (ModifiableValue<bool>)sound.IsMuted("Sound");
                    m_MusicVolume = (ModifiableValue<float>)sound.GetVolume("Music");
                    m_SoundVolume = (ModifiableValue<float>)sound.GetVolume("Sound");
                }
            }

            /// <summary>
            /// 保存设置
            /// </summary>
            public override void Save()
            {
                SoundComponent sound = LSEntry.Sound;

                if (sound != null)
                {
                    if (m_MuteMusic.IsValueChanged)
                        sound.Mute("Music", m_MuteMusic.GetValueAndClearChangedState());

                    if (m_MuteSound.IsValueChanged)
                    {
                        sound.Mute("Sound", m_MuteSound.GetValueAndClearChangedState());
                        sound.Mute("UISound", m_MuteSound.GetValueAndClearChangedState());
                    }

                    if (m_MusicVolume.IsValueChanged)
                        sound.SetVolume("Music", m_MusicVolume.GetValueAndClearChangedState());

                    if (m_SoundVolume.IsValueChanged)
                    {
                        sound.SetVolume("Sound", m_SoundVolume.GetValueAndClearChangedState());
                        sound.SetVolume("UISound", m_SoundVolume.GetValueAndClearChangedState());
                    }
                }
            }
        }
    }
}
